import { Component } from 'react';
import { Card, Form, Input, Button, Checkbox, message } from 'antd';
import logoSrc from 'assets/logo.png';
import styles from './index.module.scss';
import { loginAPI } from '../../api/user';
import { setToken } from 'utils/storage';
// console.log(styles);
export default class Login extends Component {
  // 登陆
  onFinish = async (value) => {
    try {
      const res = await loginAPI(value);
      setToken(res.data.token);
      // localStorage.setItem('geek-park', res.data.token);
      this.props.history.push('/');
      message.success('登录成功', 1);
    } catch (error) {
      message.warning(error.response.data.message, 1);
    }
  };
  render() {
    return (
      <div className={styles.login}>
        <Card className="login-container">
          <img src={logoSrc} alt="" className="login-logo" />
          <Form
            autoComplete="off"
            onFinish={this.onFinish}
            initialValues={{
              mobile: '13811111112',
              code: '246810',
              agree: true,
            }}
          >
            <Form.Item
              name="mobile"
              rules={[
                { required: true, message: '手机号码不能为空' },
                {
                  pattern: /^1[3-9]\d{9}$/,
                  message: '请输入11位手机号码',
                },
              ]}
            >
              <Input placeholder="请输入手机号码" />
            </Form.Item>

            <Form.Item
              name="code"
              rules={[
                { required: true, message: '密码不能为空' },
                {
                  pattern: /^\d{6}$/,
                  message: '请输入6位手机号码',
                },
              ]}
            >
              <Input placeholder="请输入6位验证码" />
            </Form.Item>

            <Form.Item
              name="agree"
              valuePropName="checked"
              rules={[
                {
                  validator: (_, value) =>
                    value
                      ? Promise.resolve()
                      : Promise.reject(new Error('Should accept agreement')),
                },
              ]}
            >
              <Checkbox>我已阅读并同意[隐私条款]和[用户协议]</Checkbox>
            </Form.Item>

            <Form.Item>
              <Button type="primary" block htmlType="submit">
                登陆
              </Button>
            </Form.Item>
          </Form>
        </Card>
      </div>
    );
  }
}
